<?php

class RegistrationController extends Zend_Controller_Action{
	
	public function init()
	{
		$title ="Sign Up to our web site";
		$this->view->__set("headtitle", $title);
	}
	
	public function indexAction(){
		if(Zend_Auth::getInstance()->hasIdentity()){
			$this->_redirect('index');
		}
		$this->view->form = $form = new Application_Form_Registration();
		//$form->setDecorators( array( array('ViewScript', array('viewScript' => 'login/loginform2.phtml'))));
		if($post = $this->_request->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData) AND $form->getElement("submit")->getAttrib("test") == "registration"){
				$user = new Application_Model_User($form->getValues());
                $mapper  = new Application_Model_UserMapper();
                $mapper->save($user);
                
                //Envoi de mail pour activation du compte
                $smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', Zend_Registry::get('Mail_Config'));
                $mail = new Zend_Mail('utf-8');
                $mail	->addTo($formData['email'])
                ->setFrom('joseph2033@gmail.com', 'WatchMyDesk Support')
                ->setSubject('Bienvenue sur WatchMyDesk')
                ->setBodyHtml('
                
                		Dear '.$formData['firstName'].' '.$formData['lastName'].'
                
                		Welcome to WatchMyDesk !!
                
                		Please visit this url to activate your account: 
                		'.$_SERVER['HTTP_HOST'].'/quickstart/public/registration/activate/id/'.sha1($formData['email'].$_SERVER['REMOTE_ADDR'].$formData['password']).'
                
                		See you there,
                		The WatchMyDesk Team');
                $mail->send($smtpConnection);
                
                return $this->_helper->redirector();
			}else{
				$form->populate($formData);
			}
		}
		
	}
	
	public function activateAction()
	{
		$token = $this->getRequest()->getParam('id');
		if($token){
			$mapper = new Application_Model_UserMapper();
			$mapper->validateUserByToken($token);
		}
		$this->_redirect('/');
	}
}